// pages/home/home.js
const app = getApp()
Page({
  data: {
    imgurl: app.globalData.imgurl,
    statusBar: app.globalData.statusBar,
    customBar: app.globalData.customBar,
    custom: app.globalData.custom,
    scrollRate: 0,
    city: [],
    list: [],
    active: 0,
  },
  onLoad() {
    this._getArea()
  },
  // 获取当前门店
  async _getArea() {
    let { code, data } = await wx.requestAsync({
      url: '/shop/area',
    })
    if (code == 0) {
      // console.log(data)
      this.setData({
        city: data,
      })
      this._cityList()
    }
  },
  // tabs切换
  _tapTabs(e) {
    let { index } = e.detail
    let { active } = this.data
    this.setData({
      active: index,
    })
    if (active != index) {
      this._cityList()
    }
  },
  // 获取当前城市下的列表
  async _cityList() {
    let { active, city,imgurl } = this.data
    let { code, data } = await wx.requestAsync({
      url: '/shop',
      data: {
        page: 1,
        type: city[active].type,
        code: city[active].code,
      },
    })
    if (code == 0) {
      let list = data.map((item) => {
        item.pic = imgurl = item.pic
        item.price = item.price / 100
        item.dz = item.p_name + item.c_name + item.a_name + item.address
        return item
      })
      // console.log(list)
      this.setData({ list })
    }
  },
  //去往门店介绍
  _toLink(e) {
    let s_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/storeIntroduction/storeIntroduction?s_id=' + s_id,
    })
  },
  // 拼组列表
  _toReservationList() {
    wx.navigateTo({
      url: '/pages/reservationList/reservationList',
    })
  },
  // 亲子预约列表
  _toParentchild() {
    wx.navigateTo({
      url: '/pages/parentchild/parentchild',
    })
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    let that = this

    let scrollH = that.data.swiperH

    if (e.scrollTop <= scrollH) {
      this.setData({
        scrollRate: e.scrollTop / scrollH,
      })
    } else {
      this.setData({
        scrollRate: 1,
      })
    }
  },
})
